// my/wx.js
import {
  InvioceModel
} from '../../../models/invioce'
import {
  PayModel
} from '../../../models/pay'
import {
  Format
} from '../../../utils/common'
const payModel = new PayModel()
const invioceModel = new InvioceModel()
const app = getApp()
let last
const format = new Format()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    invoiceLists: '',
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },
  onShow: function () {
    console.log('1')
    this.invoiceTileList()
    // this.loadMyAccount()
  },
  invoiceTileList() {
    invioceModel.invoiceTileList().then(res => {
      console.log(res)
      if (res && res.code == '00') {
        this.setData({
          'invoiceLists': res.titles
        })
      } else {
        if (res && res.msg) {
          wx.showToast({
            type: 'none',
            content: res.msg,
            duration: 1000,
          });
        } else {
          wx.showToast({
            type: 'none',
            content: '查询发票抬头失败',
            duration: 1000,
          });
        }
        
      }
      
    })
  },
  toPage(e) {
    const page = e.currentTarget.dataset.url
    wx.navigateTo({
      url: page
    })
  },
  toDetail(e) {
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/invioce/invioceAdd/invioceAdd?id=' + id
    })
    console.log(e)

  },
  toDel(e) {
    let id = e.currentTarget.dataset.id
    invioceModel.deleteInvoiceTitle(id).then(res => {
      console.log(res)
      if (res.code == '00') {
        wx.showToast({
          type: 'none',
          content: '删除成功',
          duration: 1000,
        });
        this.invoiceTileList()
      } else {
        wx.showToast({
          type: 'none',
          content: '删除失败',
          duration: 1000,
        });
      }
    })
  },
  //  更改默认邮箱
  saveInvoiceTitle (e) {
    let detail = e.currentTarget.dataset.item
    let index = e.currentTarget.dataset.index
    if (detail.state == 1) {
      detail.state = 0
    } else {
      detail.state = 1
    }
    invioceModel.saveInvoiceTitle(detail.id,detail.type,detail.name,detail.gmfNsrsbh,detail.gmfDz,detail.gmfDh,detail.gmfYh,detail.gmfYhzh,detail.remarks,1,detail.state).then(res => {
      console.log(res)
      if (res && res.code == '00') {
        wx.showToast({
          type: 'none',
          content: '更改抬头成功',
          duration: 1000,
        });
        if (detail.state == 1) {
          for (var i = 0; i < this.data.invoiceLists.length; i++) {
            this.data.invoiceLists[i].state = 0          
          }
          this.data.invoiceLists[index].state = 1
        } else {
          this.data.invoiceLists[index].state = 0
        }
        
        this.setData({
          'invoiceLists': this.data.invoiceLists
        })
        
      } else {
        if (res && res.ms) {
          wx.showToast({
            type: 'none',
            content: res.msg,
            duration: 1000,
          });
        } else {
          wx.showToast({
            type: 'none',
            content: '更改抬头失败',
            duration: 1000,
          });
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  // onShow: function () {
  //   // this.loadMyAccount()
  // },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})